package com.winz.cultureapp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.ProjectAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ProjectListBean;
import com.winz.cultureapp.filter.art.ArtPopWindow;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.MarginDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 艺术鉴赏
 */
public class ArtAppreciateActivity extends BaseAppCompatActivity implements XRecyclerView.LoadingListener, OnItemClickListener, ArtPopWindow.OnFinishListener {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.xrcv_list)
    XRecyclerView mXrcv_list;
    @BindView(R.id.iv_right)
    ImageView mIv_right;
    @BindView(R.id.tv_pic)
    TextView mTv_right;

    private boolean isRefresh = true;
    private int limit = 12;
    private int page = 1;
    private int totalPage = 0;

    private List<ProjectListBean.DataBean.ListBean> mDatas;
    private ProjectAdapter mAdapter;

    private ArtPopWindow mPop_public;

    private String categoryName;
    private String artCategoryId = "0";

    @Override
    protected int contentView() {
        return R.layout.activity_art_appreciate;
    }

    @Override
    protected void initViews() {
        setStatusBar(Color.WHITE);
        mTv_top_title.setText(getString(R.string.home_item6_title));
        mIv_right.setImageResource(R.drawable.icon_right_xx);
        mTv_right.setText("全部");
        mIv_right.setVisibility(View.VISIBLE);
        mTv_right.setVisibility(View.VISIBLE);

        mDatas = new ArrayList<>();
        // XrecyclerView
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        mXrcv_list.addItemDecoration(new MarginDecoration(mXrcv_list, getResources().getDimensionPixelOffset(R.dimen.layout_padding_small), 3));
        mXrcv_list.setLayoutManager(manager);
        mXrcv_list.setPullRefreshEnabled(true);
        mXrcv_list.setLoadingMoreEnabled(true);

        mAdapter = new ProjectAdapter(this, mDatas, R.layout.empty_view, R.layout.item_project);

        // 初始化
        mPop_public = new ArtPopWindow(this);
        mPop_public.setOnFinishListener(this);
    }

    @Override
    protected void initEvents() {

        mXrcv_list.setAdapter(mAdapter);
        mXrcv_list.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);

    }

    @Override
    protected void loadDatas(Intent intent) {
        getList();
    }

    /**
     * 获取数据列表
     */


    private void getList() {
        showProgressDialog();

        HttpUitls.onGet("art", RequestFactory.getInstance().art(page + "", limit + "",artCategoryId), new OnResponeListener<ProjectListBean>() {
            @Override
            public void onSuccess(ProjectListBean bean) {
                Logger.d("art==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    totalPage = bean.totalPage;
                    if (bean.data.list != null && bean.data.list.size() > 0) {
                        mXrcv_list.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = bean.data.list;
                        } else {
                            mDatas.addAll(bean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        ProjectListBean.DataBean.ListBean empty = new ProjectListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_list.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(bean.msg);
                }
                if (isRefresh) {
                    mXrcv_list.refreshComplete();
                } else {
                    mXrcv_list.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("art==>>", error);
                if (isRefresh) {
                    mXrcv_list.refreshComplete();
                } else {
                    mXrcv_list.loadMoreComplete();
                }
            }
        });
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getList();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page >= totalPage) {
            mXrcv_list.setNoMore(true, true, "没有更多数据了");
        } else {
            page++;
            getList();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.ART.ITEM_ID, mDatas.get(position).artId);
        bundle.putString(Constants.KEY.ART.ITEM_TITLE, mDatas.get(position).title);
        startActivity(ArtAppreciateListActivity.class, false, bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }


    @OnClick({R.id.fl_back,R.id.rl_right})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_back:
                finish();
                break;
            case R.id.rl_right:
                // 弹出筛选框
                mPop_public.showAsDropDown(view);
                break;
        }
    }

    @Override
    public void onFinish(String data) {
        categoryName = data.split(",")[0];
        artCategoryId = data.split(",")[1];
        mTv_right.setText(categoryName);
        getList();
    }
}
